package com.zoe.onelink.circuitbreaker.checker;

import com.zoe.onelink.circuitbreaker.property.OnelinkCircuitBreakerProperties;
import com.zoe.onelink.circuitbreaker.sentinel.OnelinkSentinelCircuitBreakerFactory;
import com.zoe.onelink.plugin.check.AbstractEnvironmentChecker;
import com.zoe.onelink.plugin.check.model.PropertyCheckResult;
import com.zoe.onelink.util.StrUtil;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.cloud.client.circuitbreaker.CircuitBreakerFactory;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>标题: </p>
 * <p>描述: </p>
 * <p>版权: Copyright (c) 2023</p>
 * <p>公司: 智业软件股份有限公司</p>
 *
 * @version: 1.0
 * @author: ruansheng
 * @date: 2023-01-17
 */
@RequiredArgsConstructor
@Slf4j
public class OnelinkSentinelCircuitBreakerChecker extends AbstractEnvironmentChecker {

    @Override
    protected List<PropertyCheckResult<?>> doCheck() {
        List<PropertyCheckResult<?>> propertyCheckResults = new ArrayList<>();
        // Onelink熔断器功能启用状态
        String circuitBreakerEnabledKey = OnelinkCircuitBreakerProperties.PREFIX + ".enabled";
        boolean isOnelinkCircuitBreakerEnabled = this.getProperty(circuitBreakerEnabledKey, Boolean.class, true);
        if (!isOnelinkCircuitBreakerEnabled) {
            PropertyCheckResult<Object> result = new PropertyCheckResult<>()
                    .setLevel(PropertyCheckResult.Level.DANGER)
                    .setMessage("Onelink熔断器功能已关闭")
                    .setPropKey(circuitBreakerEnabledKey)
                    .setPropValue(isOnelinkCircuitBreakerEnabled);
            propertyCheckResults.add(result);
        }
        CircuitBreakerFactory circuitBreakerFactory = this.getBean(CircuitBreakerFactory.class);
        boolean usingOnelinkCircuitBreakerFactory = circuitBreakerFactory instanceof OnelinkSentinelCircuitBreakerFactory;
        if (isOnelinkCircuitBreakerEnabled && circuitBreakerFactory != null && !usingOnelinkCircuitBreakerFactory) {
            PropertyCheckResult<Object> result = new PropertyCheckResult<>()
                    .setLevel(PropertyCheckResult.Level.SEVERITY)
                    .setMessage("Onelink熔断器功能已启用，但是没有使用[OnelinkSentinelCircuitBreakerFactory]作为熔断器工厂")
                    .setPropKey(circuitBreakerEnabledKey)
                    .setPropValue(isOnelinkCircuitBreakerEnabled);
            propertyCheckResults.add(result);
        }
        if (!isOnelinkCircuitBreakerEnabled && circuitBreakerFactory != null) {
            String msg = StrUtil.format("Onelink熔断器功能未启用，但是存在熔断器工厂[{}]实例",
                    circuitBreakerFactory.getClass().getName());
            PropertyCheckResult<Object> result = new PropertyCheckResult<>()
                    .setLevel(PropertyCheckResult.Level.SEVERITY)
                    .setMessage(msg)
                    .setPropKey(circuitBreakerEnabledKey)
                    .setPropValue(isOnelinkCircuitBreakerEnabled);
            propertyCheckResults.add(result);
        }
        return propertyCheckResults;
    }
}
